package redisclient

import (
	"gitee.com/lsy007/redisclient/model"
	"github.com/pquerna/ffjson/ffjson"
)

// Set(key string, value string, t int64) (string,err error) {}
// SetNX(key string, value string, t int64) (bool,err error) {}
// Get(key string) (string,err error) {}
// GetRange(key string, start int64, end int64) (string,err error) {}
// GetSet(key string, value string) (string,err error) {}
// SetBit(key string, offset int64, value int64) (int64,err error) {}
// GetBit(key string, offset int64) (int64,err error) {}
// MGet(keys... string) ([]string,err error) {}
// SetRange(key string, offset int64, value string) (int64,err error) {}
// StrLen(key string) (int64,err error) {}
// MSet(keyValue... string) (string,err error) {}
// MSetNX(keyValue... string) (bool,err error) {}
// Incr(key string) (int64,err error) {}
// IncrBy(key string, incr int64) (int64,err error) {}
// IncrByFloat(key string, incr float64) (float64,err error) {}
// Decr(key string) (int64,err error) {}
// DecrBy(key string, decr int64) (int64,err error) {}
// Append(key string, value string) (int64,err error) {}

// 设置指定 key 的值，设置过期时间，0 则为永不过期
func (c *RedisClient) Set(key string, value string, t int64) (status string, err error) {
	args := &model.RedisArgsModel{Key: key, Value: value, Time: t}
	response, err = c.Request("Set", "String", args)
	return response.String, err
}

// 只有在 key 不存在时设置 key 的值
func (c *RedisClient) SetNX(key string, value string, t int64) (status bool, err error) {
	args := &model.RedisArgsModel{Key: key, Value: value, Time: t}
	response, err = c.Request("SetNX", "String", args)
	return response.Bool, err
}

// 获取指定 key 的值
func (c *RedisClient) Get(key string) (value string, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("Get", "String", args)
	return response.String, err
}

// 返回 key 中字符串值的子字符
func (c *RedisClient) GetRange(key string, start int64, end int64) (child string, err error) {
	args := &model.RedisArgsModel{Key: key, StartInt: start, EndInt: end}
	response, err = c.Request("GetRange", "String", args)
	return response.String, err
}

// 将给定 key 的值设为 value ，并返回 key 的旧值(old value)
func (c *RedisClient) GetSet(key string, value string) (old string, err error) {
	args := &model.RedisArgsModel{Key: key, Value: value}
	response, err = c.Request("GetSet", "String", args)
	return response.String, err
}

// 对 key 所储存的字符串值，设置或清除指定偏移量上的位(bit)
func (c *RedisClient) SetBit(key string, offset int64, value int64) (count int64, err error) {
	args := &model.RedisArgsModel{Key: key, Offset: offset, BitValue: value}
	response, err = c.Request("SetBit", "String", args)
	return response.Int, err
}

// 对 key 所储存的字符串值，获取指定偏移量上的位(bit)
func (c *RedisClient) GetBit(key string, offset int64) (bit int64, err error) {
	args := &model.RedisArgsModel{Key: key, Offset: offset}
	response, err = c.Request("GetBit", "String", args)
	return response.Int, err
}

// 获取所有(一个或多个)给定 key 的值
func (c *RedisClient) MGet(keys ...string) (values []interface{}, err error) {
	args := &model.RedisArgsModel{Keys: keys}
	response, err = c.Request("MGet", "String", args)
	values = make([]interface{}, 0)
	if response.Bytes != nil {
		err = ffjson.Unmarshal(response.Bytes, &values)
	}
	return
}

// 用 value 参数覆写给定 key 所储存的字符串值，从偏移量 offset 开始
func (c *RedisClient) SetRange(key string, offset int64, value string) (len int64, err error) {
	args := &model.RedisArgsModel{Key: key, Offset: offset, Value: value}
	response, err = c.Request("SetRange", "String", args)
	return response.Int, err
}

// 返回 key 所储存的字符串值的长度
func (c *RedisClient) StrLen(key string) (len int64, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("StrLen", "String", args)
	return response.Int, err
}

// 同时设置一个或多个 key-value 对
func (c *RedisClient) MSet(keyValues ...interface{}) (status string, err error) {
	var bytes []byte
	if bytes, err = ffjson.Marshal(keyValues); err != nil {
		return
	}
	args := &model.RedisArgsModel{Bytes: bytes}
	response, err = c.Request("MSet", "String", args)
	return response.String, err
}

// 同时设置一个或多个 key-value 对，当且仅当所有给定 key 都不存在
func (c *RedisClient) MSetNX(keyValues ...interface{}) (status bool, err error) {
	var bytes []byte
	if bytes, err = ffjson.Marshal(keyValues); err != nil {
		return
	}
	args := &model.RedisArgsModel{Bytes: bytes}
	response, err = c.Request("MSetNX", "String", args)
	return response.Bool, err
}

// 将 key 中储存的数字值增一
func (c *RedisClient) Incr(key string) (value int64, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("Incr", "String", args)
	return response.Int, err
}

// 将 key 所储存的值加上给定的增量值
func (c *RedisClient) IncrBy(key string, incr int64) (value int64, err error) {
	args := &model.RedisArgsModel{Key: key, IncrInt: incr}
	response, err = c.Request("IncrBy", "String", args)
	return response.Int, err
}

// 将 key 所储存的值加上给定的浮点增量值
func (c *RedisClient) IncrByFloat(key string, incr float64) (value float64, err error) {
	args := &model.RedisArgsModel{Key: key, IncrFloat: incr}
	response, err = c.Request("IncrByFloat", "String", args)
	return response.Float, err
}

// 将 key 中储存的数字值减一
func (c *RedisClient) Decr(key string) (value int64, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("Decr", "String", args)
	return response.Int, err
}

// key 所储存的值减去给定的减量值
func (c *RedisClient) DecrBy(key string, decr int64) (value int64, err error) {
	args := &model.RedisArgsModel{Key: key, DecrInt: decr}
	response, err = c.Request("DecrBy", "String", args)
	return response.Int, err
}

// 如果 key 不存在，则添加键值；如果已经存在并且是一个字符串， APPEND 命令将指定的 value 追加到该 key 原来值（value）的末
func (c *RedisClient) Append(key string, value string) (len int64, err error) {
	args := &model.RedisArgsModel{Key: key, Value: value}
	response, err = c.Request("Append", "String", args)
	return response.Int, err
}
